package com.sjf.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjf.common.Result;
import com.sjf.reggie.dto.SetmealDto;
import com.sjf.reggie.entity.Dish;
import com.sjf.reggie.entity.Setmeal;
import com.sjf.reggie.service.SetmealDishService;
import com.sjf.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐 控制器
 *
 * @author <huangtang>
 * @since 2022/4/26 15:59
 **/
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> save(@RequestBody SetmealDto setmealDto ){
        log.info ("新增套餐:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return Result.success ("新增套餐成功");
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealDto> getById(@PathVariable("id")Long id){
       SetmealDto setmealDto = setmealService.getSetmealWithDish (id);
       return Result.success (setmealDto);
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     * @return
     */
    @PutMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> editSetmeal(@RequestBody SetmealDto setmealDto){
        log.info ("修改套餐信息：{}",setmealDto);
         setmealService.editSetmeal(setmealDto);
        return  Result.success ("修改套餐成功");
    }

    /**
     * 套餐列表分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(@RequestParam(value = "page",defaultValue = "1")Integer page,
                             @RequestParam(value = "pageSize")Integer pageSize,
                             String name){
        Page result = setmealService.getList(page,pageSize,name);
        return Result.success (result);
    }

    /**
     * 根据id删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping("")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> deleteByIds(String ids){
        log.info ("删除套餐:{}",ids);
        boolean result = setmealService.deleteByIds(ids);
        return result?Result.success ("删除套餐成功"):Result.error ("删除套餐失败");
    }
    /**
     * 修改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable("status")Integer status,String ids){
         setmealService.updateStatus(status,ids);
        return Result.success ("修改售卖状态成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public Result<List<Setmeal>> getSetmealDishList( Setmeal setmeal){
        List<Setmeal> setmealDishList = setmealService.getSetmealDishList(setmeal);
        return Result.success (setmealDishList);
    }
}
